package com.lanqiao.gxnushop.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanqiao.gxnushop.entity.Buyers;
import com.lanqiao.gxnushop.entity.Stores;
import com.lanqiao.gxnushop.service.StoresService;
import com.lanqiao.gxnushop.util.UUIdUtil;
import com.lanqiao.gxnushop.valueObject.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.io.File;
import java.util.ArrayList;

@Log4j
@RestController
public class StoresController {
    @Autowired
    private StoresService storesService;

    @Operation(summary = "增加店铺 需要传入的参数有：store（sellerId：卖家Id， name:店铺名, description:店铺描述)  state:店铺状态默认为0：申请状态返回值  boolean ")
    @Parameter(name = "stores",description = "店铺信息")
    @PostMapping("/storesCtrl/add")
    public boolean add(Stores stores){
        //生成storeId作为主键
        stores.setStoreId(UUIdUtil.getUserId());
        stores.setState("0");
        return storesService.add(stores);
    }
    @Operation(summary = "通过店铺编号：storeId 删除店铺  返回值：boolean")
    @DeleteMapping("storesCtrl/delete")
    public boolean delete(String storeId){
        return storesService.delete(storeId);
    }

    @Operation(summary = "通过店铺编号：storeId 修改店铺信息(可修改的有name description state) 返回值：boolean")
    @Parameter(name = "stores",description = "店铺信息")
    @PutMapping("storesCtrl/update")
    public boolean update(Stores stores){
        return storesService.update(stores);
    }

    @Operation(summary = "精确查找某个店铺   用于 ！！！管理员！！！的管理  返回值实体stores")
    @Parameter(name = "storeId",description = "店铺编号")
    @GetMapping("storesCtrl/select")
    public Stores select(String storeId){
        return storesService.select(storeId);
    }

    @Operation(summary = "卖家名下的店铺 用于卖家查看自己的所有店铺  传入参数sellerId,page ;  返回值PageInfo<Stores>")
    @Parameter(name = "sellerId",description = "卖家Id")
    @GetMapping("storesCtrl/selectBySellerId")
    public PageInfo<Stores> selectBySellerId(String sellerId,Page page){
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        ArrayList list = storesService.selectBySellerId(sellerId);
        return new PageInfo<Stores>(list);
    }


    @Operation(summary = "通过店名进行模糊查找; 传入参数name,page ;  返回值ArrayList<Stores>")
    @Parameter(name = "name",description = "店铺名")
    @GetMapping("storesCtrl/selectByName")
    public PageInfo<Stores> selectByName(String name, Page page){
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        ArrayList list = storesService.selectByName(name);
        return new PageInfo<Stores>(list);
    }

    @Operation(summary = "上传店铺头像")
    @Parameter(name="pic",description = "用户头像图片" ,content = @Content(mediaType = "multipart/form-data")  )
    @Parameter(name="storeId",description = "用户编号")
    @PostMapping("/storesCtrl/upload")
    public String upload(@RequestPart("pic") MultipartFile pic, @RequestParam("storeId") String storeId)  {
        String fileName = null;
        log.debug("--------------------------" + storeId);
        try {
            if (!pic.isEmpty() && ImageIO.read(pic.getInputStream()) != null) {
                //获取文件名
                fileName = pic.getOriginalFilename();
                //通过UUID 设置新的文件名
                fileName = UUIdUtil.getUserId() + fileName.substring(fileName.lastIndexOf("."));
                //创建目标文件
                File file = new File(fileName);
                //将照片保存到目标文件
                pic.transferTo(file);

                //将图片名保存到数据库
                Stores stores = new Stores();
                stores.setStoreId(storeId);
                stores.setLogoUrl(fileName);
                log.debug("--------------------------" + stores);
                storesService.updateStoreImg(stores);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return fileName;
    }

}
